def PR(n):
    d = 2
    while d < n:
        if n % d == 0:
            n = -1
            break
        d += 1
    if n < 2:
        n = -1
    return n
n = int(input())
if n <= 999:
    print(0)
    exit()
if n > 9999:
    print(0)
    exit()
las = n % 10
pre = (n // 10) % 10
sec = (n // 100) % 10
fir = n // 1000
las_pre = n % 100
pre_sec = (n // 10) % 100
fir_sec = n // 100
fir_sec_pre = n // 10
sec_pre_las = n % 1000
n = PR(n)
las = PR(las)
pre = PR(pre)
sec = PR(sec)
fir = PR(fir)
las_pre = PR(las_pre)
pre_sec = PR(pre_sec)
fir_sec = PR(fir_sec)
fir_sec_pre = PR(fir_sec_pre)
sec_pre_las = PR(sec_pre_las)
if las == -1 and n == -1 and pre == -1 and sec == -1 and fir == -1 and las_pre == -1 and pre_sec == -1 and fir_sec == -1 and fir_sec_pre == -1 and sec_pre_las == -1:
    print(0)
else:
    print(max(las,pre,sec,fir,las_pre,pre_sec,fir_sec,fir_sec_pre,sec_pre_las,n))